package com.ecar.api.service.order.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.api.service.common.FlightService;
import com.ecar.api.service.order.OrderPriceService;
import com.ecar.api.support.dto.Constants;
import com.ecar.api.support.dto.FlightExistInfo;
import com.ecar.api.support.util.DateUtil;
import com.ecar.api.support.util.Redis;
import com.ecar.common.dto.DataBean;
import com.ecar.common.util.JSONUtil;
import com.ecar.core.order.dao.OrderPriceRuleMapper;
import com.ecar.core.order.entity.OrderPriceRule;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

@Service
@Transactional
public class OrderPriceServiceImpl implements OrderPriceService {

	@Autowired
	OrderPriceRuleMapper orderPriceRuleMapper;
	
	@Autowired
	FlightService flightService;

	@Override
	public OrderPriceRule queryOrderPriceRule(int carLevel, int serviceType) {
		return orderPriceRuleMapper.queryOrderPriceRule(carLevel, serviceType);
	}

	@Override
	public DataBean getJSPrePrice(JSONObject fields) {

		try {
			// 接送机价格
			Map<String, Object> map = new HashMap<>();

			// 两位小数,因为前端传的可能超过两位
			DecimalFormat df = new DecimalFormat("######0.00");

			// 车辆级别
			int carLevel = fields.getInt("carLevel");
			// 服务类型
			int serviceType = fields.getInt("serviceType");
			// 预估时间
			int preTime = fields.getInt("preTime");
			// 预估里程
			double distance = fields.getDouble("distance");
			// 向上取整,不足一公里视为一公里
			distance = Math.ceil(distance);
			
			Integer hour = null;
			//航班判断
			if (Constants.RECEIVE_TYPE == serviceType) {
				String flightNo = fields.getString("flightNo");
				String date = fields.getString("date");
				DataBean bean = flightService.getFlightInfo(flightNo, date);
				if (bean.getCode() == 200) {
					//当航班存在的时候,将延迟接机的分钟数加上就是接机时间
					String timeStamp = fields.getString("timeStamp");
					long arriveTime = ((FlightExistInfo)(bean.getResult())).getArrScheduled().getTime();
					Long time = arriveTime+Long.valueOf(timeStamp);
					hour = DateUtil.getHourByTimeStamp(String.valueOf(time));
					map.put("flightInfo", bean.getResult());
				}else {
					return bean;
				}
			}
				
			//送机的时候
			if (Constants.SEND_TYPE == serviceType) {
				String timeStamp = fields.getString("timeStamp");
				hour = DateUtil.getHourByTimeStamp(timeStamp);
			}
			

			// 计价规则
			OrderPriceRule orderPriceRule = orderPriceRuleMapper.queryOrderPriceRule(carLevel, serviceType);

			// 套餐价
			Double minPrice = orderPriceRule.getMinPrice();

			// 套餐包含里程
			Double menuRate = orderPriceRule.getMenuRate();
			// 套餐包含时间(元/分钟)
			Double menuTime = orderPriceRule.getMenuTime();

			// 超里程单价
			Double passRate = 0.0;
			// 超里程价
			Double passRatePrice = 0.0;
			// 超过里程数
			Double passDistance = 0.0;
			// 如果超过里程了
			if (distance > menuRate) {
				passRate = orderPriceRule.getPassRate();
				passDistance = distance - menuRate;
				passRatePrice = passDistance * passRate;
			}
			

			// 超时单价(元/每分钟)
			Double passTime = 0.0;
			// 超时价
			Double passtimePrice = 0.0;

			if (preTime > menuTime) {
				passTime = orderPriceRule.getPassTime();
				passtimePrice = (preTime - menuTime) * passTime;
			}

			map.put("passtimePrice", df.format(passtimePrice));

			// 夜间价
			Double nightMoney = 0.0;

			// 判断是否算夜间费(晚上22点到早上6点)
			Boolean flag = null;
			if (hour >= 22 || hour < 6) {
				flag = true;
			} else {
				flag = false;
			}

			// 接送机套餐外的里程才算夜间价
			if (flag) {
				nightMoney = passDistance * (orderPriceRule.getNightPrice());
			}

			map.put("prePrice", df.format(minPrice + passRatePrice + passtimePrice + nightMoney));
			map.put("minPrice", df.format(minPrice));
			map.put("nightMoney", df.format(nightMoney));
			map.put("passRatePrice", df.format(passRatePrice));

			return new DataBean(200, map, "调用成功返回预估价格详情!");
		} catch (Exception e) {
			e.printStackTrace();
			return new DataBean(401, new JSONObject(), "参数错误!!");
		}
	}

	@Override
	public DataBean getKPDepPrice(JSONObject fields) {
		try {
			// 快车价格
			Map<String, Object> kmap = new HashMap<>();

			// 拼车价格
			Map<String, Object> pmap = new HashMap<>();

			// 两位小数,因为前端传的可能超过两位
			DecimalFormat df = new DecimalFormat("######0.00");

			// 车辆级别
			int carLevel = fields.getInt("carLevel");

			// 服务类型
			int serviceType = fields.getInt("serviceType");

			// 预估里程
			double distance = fields.getDouble("distance");
			
			// 预估时间
			int preTime = fields.getInt("preTime");

			pmap.put("distance", distance);

			int useSeats = fields.getInt("useSeats");

			// 乘客id
			String passengerId = String.valueOf(fields.getInt("passengerId"));

			// 向上取整,不足一公里视为一公里
			distance = Math.ceil(distance);

			OrderPriceRule orderPriceRule = orderPriceRuleMapper.queryOrderPriceRule(carLevel, serviceType);

			// 套餐价
			Double minPrice = orderPriceRule.getMinPrice();

			// 套餐包含里程
			Double menuRate = orderPriceRule.getMenuRate();
			// 套餐包含时间(元/分钟)
			Double menuTime = orderPriceRule.getMenuTime();

			// 超里程单价
			Double passRate = 0.0;
			// 超里程价
			Double passRatePrice = 0.0;
			// 超过里程数
			Double passDistance = 0.0;
			// 如果超过里程了
			if (distance > menuRate) {
				passRate = orderPriceRule.getPassRate();
				passDistance = distance - menuRate;
				passRatePrice = passDistance * passRate;
			}

			// 夜间价
			Double nightMoney = 0.0;

			// 判断是否算夜间费(晚上22点到早上6点)
			Boolean flag = null;
			Integer hour = null;
			if (Constants.FAST_FUTURE == serviceType) {
				String timeStamp = fields.getString("timeStamp");
				hour = DateUtil.getHourByTimeStamp(timeStamp);
			}else{
				 hour = DateUtil.getCurrentHour();
			}
			if (hour >= 22 || hour < 6) {
				flag = true;
			} else {
				flag = false;
			}

			// 超时长价格(目前上车就算时长费)
			Double timePrice = (preTime-menuTime)*orderPriceRule.getPassTime();
			
			// 远途费(目前超过15km每公里多收1元)
			Double longDistanceRate = orderPriceRule.getLongDistanceRate();
			Double longDistancePrice = 0.00;
			if (distance>longDistanceRate) {
				longDistancePrice = (distance-orderPriceRule.getLongDistanceRate())*orderPriceRule.getLongDistancePrice();
			}
			
			// 接送机套餐外的里程才算夜间价
			if (flag) {
				nightMoney = passDistance * (orderPriceRule.getNightPrice());
			}

			kmap.put("prePrice", df.format(minPrice + passRatePrice + nightMoney+timePrice+longDistancePrice));
			kmap.put("minPrice", df.format(minPrice));
			kmap.put("nightMoney", df.format(nightMoney));
			kmap.put("passRatePrice", df.format(passRatePrice));
			kmap.put("timePrice", df.format(timePrice));
			kmap.put("longDistancePrice", df.format(longDistancePrice));

			pmap.put("minPrice", df.format(minPrice));
			pmap.put("fnightMoney", df.format(nightMoney * 0.9));
			pmap.put("ftimePrice", df.format(timePrice * 0.9));
			pmap.put("fpassRatePrice", df.format(passRatePrice * 0.9));
			pmap.put("fprePrice", df.format(minPrice + nightMoney * 0.9 + passRatePrice * 0.9));

			switch (useSeats) {

			case 1:
				pmap.put("snightMoney", df.format(nightMoney * 0.6));
				pmap.put("spassRatePrice", df.format(passRatePrice * 0.6));
				pmap.put("stimePrice", df.format(timePrice * 0.6));
				double sprePrice = minPrice + nightMoney * 0.6 + passRatePrice * 0.6+timePrice * 0.6;
				pmap.put("sprePrice", df.format(sprePrice));
				pmap.put("canWait", df.format(sprePrice * 0.05));
				break;
			case 2:
				pmap.put("snightMoney", df.format(nightMoney * 0.65));
				pmap.put("spassRatePrice", df.format(passRatePrice * 0.65));
				pmap.put("stimePrice", df.format(timePrice * 0.65));
				double sprePrice1 = minPrice + nightMoney * 0.65 + passRatePrice * 0.65+timePrice * 0.65;
				pmap.put("sprePrice", df.format(sprePrice1));
				pmap.put("canWait", df.format(sprePrice1 * 0.05));
				break;

			case 3:
				pmap.put("snightMoney", df.format(nightMoney * 0.7));
				pmap.put("spassRatePrice", df.format(passRatePrice * 0.7));
				pmap.put("stimePrice", df.format(timePrice * 0.7));
				double sprePrice2 = minPrice + nightMoney * 0.7 + passRatePrice * 0.7+timePrice * 0.7;
				pmap.put("sprePrice", df.format(sprePrice2));
				pmap.put("canWait", df.format((sprePrice2) * 0.05));
				break;

			default:
				return new DataBean(200, new JSONObject(), "座位选择错误!");
			}

			HashMap<String, Object> main = new HashMap<String, Object>();
			/*
			 * main.put("快车价格详情", kmap); main.put("拼车价格详情", pmap);
			 */
			main.put("kcPriceDetail", kmap);
			main.put("pcPriceDetail", pmap);
			String pcInfo = pmap.toString();
			Redis.c.setex(Redis.K.CARPOOL_INFO.to(passengerId), Redis.M15, pcInfo);
			return new DataBean(200, main, "调用成功返回预估价格详情!");
		} catch (Exception e) {
			e.printStackTrace();
			return new DataBean(401, new JSONObject(), "参数错误!!");
		}
	}

}
